package com.yl.auth.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Permission;
import com.yl.auth.entity.Role;
import com.yl.auth.service.JigouzhongxinduanroleService;
import com.yl.auth.service.PermissionService;
import com.yl.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author L
 * @since 2023-07-19
 */
@RestController
@RequestMapping("/admin/yanglao/jigouzhongxinduanrole")
@Api(tags = "机构中心端-角色管理")
public class JigouzhongxinduanroleController {
    @Resource
    private JigouzhongxinduanroleService jigouzhongxinduanroleService;

    @Resource
    private PermissionService permissionService;
    @ApiOperation("全查角色管理信息")
    @GetMapping("/findAll/{page}/{limit}")
    public Result findAll(@PathVariable Integer page,@PathVariable Integer limit){
        Page<Role> page1 = new Page<>(page,limit);
        Page<Role> jigouzhongxinduanrolePage = jigouzhongxinduanroleService.findAllAndPage(page1);
        return Result.ok(jigouzhongxinduanrolePage);
    }

    @ApiOperation("查看机构中心端的菜单")
    @GetMapping("/findAllByPid")
    public Result findAllByPid(){
        List<Permission> list = permissionService.list();
        //找出一级菜单
        List<Permission> permissions = list.stream().filter(menu -> menu.getPid().equals("0")).collect(Collectors.toList());
        for (Permission menu : permissions) {
            //找出一级菜单的子菜单
            List<Permission> collect = list.stream().filter(zmenu -> zmenu.getPid().equals(menu.getId())).collect(Collectors.toList());
            menu.setChildren(collect);
            for (Permission permission : collect) {
                //找到二级菜单的子菜单
                List<Permission> collect1 = list.stream().filter(smenu -> smenu.getPid().equals(permission.getId())).collect(Collectors.toList());
                permission.setChildren(collect1);
            }
        }
        return Result.ok(permissions);
    }


    @ApiOperation("添加角色信息以及配置权限")
    @PostMapping("/saveRoleAndPermission/{idList}")
    public Result saveRoleAndPermission(@RequestBody Role role, @PathVariable @ApiParam("权限id集合") List<Long> idList){
        role.setCreateTime(LocalDateTime.now());
        jigouzhongxinduanroleService.saveRoleAndPermission(role,idList);
        return Result.ok();
    }

    @ApiOperation("查看该角色的权限信息")
    @GetMapping("/getRoleById/{id}")
    public Result getRoleById(@PathVariable @ApiParam("角色id") Long id){
        Role role = jigouzhongxinduanroleService.getById(id);
        String name = role.getName();
        List<Long> idList = jigouzhongxinduanroleService.getRoleById(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("role",role);
        map.put("idList",idList);
        return Result.ok(map);
    }

    @ApiOperation("修改前先清空该角色的权限")
    @DeleteMapping("/deletePerById/{id}")
    public Result deletePerById(@PathVariable @ApiParam("角色id") Long id){
        jigouzhongxinduanroleService.deleteById(id);
        return Result.ok();
    }
    @ApiOperation("修改角色信息")
    @PostMapping("/updateRole")
    public Result updateRoleAndPermission(@RequestBody Role role){
        jigouzhongxinduanroleService.updateById(role);
        return Result.ok();
    }
    @ApiOperation("修改角色的权限信息")
    @PostMapping("/updateRolePerById/{roleId}")
    public Result updateRolePerById(@PathVariable@ApiParam("角色id") Integer roleId, @RequestBody @ApiParam("权限id集合") List<Long> idList){
        jigouzhongxinduanroleService.updateRpByRoleId(roleId,idList);
        return Result.ok();
    }


    @ApiOperation("根据id查询角色信息")
    @GetMapping("/getByIdRole/{id}")
    public Result getByIdRole(@PathVariable Integer id){
        Role role = jigouzhongxinduanroleService.getByIdRole(id);
        return Result.ok(role);
    }
    @ApiOperation("根据id删除指定角色信息")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){

        jigouzhongxinduanroleService.removeById(id);
        //删除该角色在账号跟角色中间表的信息
        jigouzhongxinduanroleService.deleteRaByRoleId(id);
        //删除该角色权限信息
        jigouzhongxinduanroleService.deleteRpByRoleId(id);
        return Result.ok();
    }
}

